﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using DLL;


namespace BLL
{
   public class ChiTietHDBLL
    {
       DataServer DB = new DataServer();
       public DataTable Select_MaHD(string Ma)
       {
           string sql = @"SELECT ChiTietHoaDon.MaHD, ChiTietHoaDon.MaTB, ThietBi.TenTB, ChiTietHoaDon.DonGia, ChiTietHoaDon.SoLuong
                        FROM  ChiTietHoaDon INNER JOIN ThietBi ON ChiTietHoaDon.MaTB = ThietBi.MaTB WHERE (ChiTietHoaDon.MaHD = @MaHD)";
           SqlParameter paramHD = new SqlParameter("@MaHD", Ma);
           DataTable bang = DB.LayDulieu(sql, paramHD);
           return bang;
       }
       public bool Kiemtra(string Ma)
       {
           string sql = string.Format("sp_ThietBi_Kiemtra");
           SqlParameter paramMNTB = new SqlParameter("@MaTB", Ma);
           DataTable bang = DB.LayDuLieuProc(sql, paramMNTB);
           if (bang.Rows.Count > 0)
           {
               return true;
           }
           return false;
       }
       public float laygia(string Ma)
       {
           float a=0;
           string sql = string.Format("sp_ThietBi_Kiemtra");
           SqlParameter paramMNTB = new SqlParameter("@MaTB", Ma);
           DataTable bang = DB.LayDuLieuProc(sql, paramMNTB);
           if (bang.Rows.Count > 0)
           {
               a =float.Parse(bang.Rows[0]["GiaTien"].ToString());
           }
           return a;
       }
       public bool Them(ChiTietHDDAL cthd)
       {
           string sql = string.Format("sp_ChiTietHoaDon_Insert");
           SqlParameter paramMHD = new SqlParameter("@MaHD", cthd.MaHD);
           SqlParameter paramMTB = new SqlParameter("@MaTB", cthd.MaTB);
           SqlParameter paramSL = new SqlParameter("@SoLuong", cthd.SoLuong);
           SqlParameter paramDG = new SqlParameter("@DonGia", cthd.DonGia);
           //try
           //{
               DB.CapNhatProc(sql, paramMHD, paramMTB, paramSL, paramDG);

           //}
           //catch
           //{
           //    return false;
           //}
           return true;
       }
       public bool Sua(ChiTietHDDAL cthd)
       {
           string sql = string.Format("sp_ChiTietHoaDon_Update");
           SqlParameter paramMHD = new SqlParameter("@MaHD", cthd.MaHD);
           SqlParameter paramMTB = new SqlParameter("@MaTB", cthd.MaTB);
           SqlParameter paramSL = new SqlParameter("@SoLuong", cthd.SoLuong);
           SqlParameter paramDG = new SqlParameter("@DonGia", cthd.DonGia);
           try
           {
               DB.CapNhatProc(sql, paramMHD, paramMTB, paramSL, paramDG);
           }
           catch
           {
               return false;
           }
           return true;
       }
       public bool Xoa(string MaHD, string MaTB)
       {
           string sql = string.Format("sp_ChiTietHoaDon_Delete");
           SqlParameter paramMHD = new SqlParameter("@MaHD", MaHD);
           SqlParameter paramMTB = new SqlParameter("@MaTB", MaTB);
           try
           {
               DB.CapNhatProc(sql, paramMHD, paramMTB);
           }
           catch
           {
               return false;
           }
           return true;
       }
    }
}
